﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMI_TS_WAREHOUSE : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMI_TS_WAREHOUSE()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = "exec proc_biz_bizcenter 'i_ts_warehouse','','"+ uifequipmentbarcode.Text+ "',2,'"+sUser+"'";
            StaticClass.myDB.doexec(sql);
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select b.fpoint_name,c.fdictname,a.*"
              + "  from i_ts_warehouse a"
              + "  inner join s_ts_dict c on c.fdicttype = 3001 and c.fdictno = a.fstorageflag"
              + "  inner join a_ts_point b on b.fpoint_no=a.fpoint_no"
              + "  where 1 = 1";
            DataRowView drv = (DataRowView)uiCBfstorageflag.SelectedItem;
            if ((drv != null) && (uiCBfstorageflag.SelectedIndex>0))
            {
                string sfstorageflag = drv.Row["fDictNo"].ToString();
                SQL = SQL + " and c.fdictno='" + sfstorageflag + "'";
            }
            DataRowView drv2 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((drv2 != null)&&(cbfpoint_no.SelectedIndex>0))
            {
                string sfpoint_no = drv2.Row["fpoint_no"].ToString();
                SQL = SQL + " and a.fpoint_no='" + sfpoint_no + "'";
            }
            if (qifequipmentbarcode.Text != "")
            {
                SQL = SQL + " and ((fequipmentbarcode like '%" + qifequipmentbarcode.Text + "%')"
                 + " or (fequipmenttag like '%" + qifequipmentbarcode.Text + "%'))";
            }
            SQL = SQL + "  order by b.fpoint_name,c.fdictname,a.fequipmentbarcode";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMI_TS_WAREHOUSE_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_warehouse";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict2 = myCache.doGetDict("S_TS_DICT", "fDictType=3012");
            uifiptype.DataSource = myTs_Dict2;
            uifiptype.DisplayMember = "fDictName";
            uifiptype.ValueMember = "fDictNo";

            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=3001");
            uifstorageflag.DataSource = myTs_Dict;
            uifstorageflag.DisplayMember = "fDictName";
            uifstorageflag.ValueMember = "fDictNo";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            uiCBfstorageflag.DataSource = myTs_Dict;
            uiCBfstorageflag.DisplayMember = "fDictName";
            uiCBfstorageflag.ValueMember = "fDictNo";
            uiCBfstorageflag.SelectedIndex = 0;

            DataTable myTs_Point = myCache.doGetDict("A_TS_POINT", "fpoint_flag=0");
            uifpoint_no.DataSource = myTs_Point;
            uifpoint_no.DisplayMember = "fpoint_name";
            uifpoint_no.ValueMember = "fpoint_no";

            DataRow dr2 = myTs_Point.NewRow();
            dr2["fpoint_name"] = "请选择";
            dr2["fpoint_no"] = "0";
            myTs_Point.Rows.InsertAt(dr2, 0);
            cbfpoint_no.DataSource = myTs_Point;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            DataTable myTs_State = myCache.doGetDict("S_TS_EQUIPMENT_STATE", "");
            uifstate.DataSource = myTs_State;
            uifstate.DisplayMember = "fstatename";
            uifstate.ValueMember = "fstateno";

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifequipmentbarcode.Text = "";
            uifequipmenttag.Text = "";
            uifmacno.Text = "";
            uifrfidIP.Text = "";
            uifrfidPort.Text = "6000";
            uifrfidid.Text = "0";
            uifip.Text = "";
            uifport.Text = "5000";
            uiflamp1.Text = "";
            uiflamp2.Text = "";
            uiflamp3.Text = "";
            uiflamp4.Text = "";
            uiflamp5.Text = "";
            uifon.Text = "";
            uifcloseflag.Text = "";
            uifairIP.Text = "";
            uifairPORT.Text = "";
            uifNoOrder.Text = "";
            uifYesOrder.Text = "";
            uifpoint_size.Text = "";
            uifstate.SelectedIndex = 1;
            uifpoint_no.SelectedIndex = -1;
            uifstorageflag.SelectedIndex = 0;
            uifnum.Text = "0";
            uifweight.Text = "0";
            uifiptype.SelectedIndex = 1;
            uifcloseflag.Text = "";

            DataTable myNo = StaticClass.myDB.query("exec fun_barcode 1");
            if ((myNo != null) && (myNo.Rows.Count > 0))
            {
                uifequipmentbarcode.Text = myNo.Rows[0][0].ToString();
                uifmacno.Text= myNo.Rows[0][0].ToString();
            }
            sType = "A";
            doLoadData();
            uifequipmentbarcode.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifequipmentbarcode.Text = uiDGView["fequipmentbarcode", rowindex].Value.ToString();
                uifequipmenttag.Text = uiDGView["fequipmenttag", rowindex].Value.ToString();
                uifmacno.Text = uiDGView["fmacno", rowindex].Value.ToString();
                uifrfidIP.Text = uiDGView["frfidIP", rowindex].Value.ToString();
                uifrfidPort.Text = uiDGView["frfidPort", rowindex].Value.ToString();
                uifrfidid.Text = uiDGView["frfidid", rowindex].Value.ToString();
                uifip.Text = uiDGView["fip", rowindex].Value.ToString();
                uifport.Text = uiDGView["fport", rowindex].Value.ToString();
                uiflamp1.Text = uiDGView["flamp1", rowindex].Value.ToString();
                uiflamp2.Text = uiDGView["flamp2", rowindex].Value.ToString();
                uiflamp3.Text = uiDGView["flamp3", rowindex].Value.ToString();
                uiflamp4.Text = uiDGView["flamp4", rowindex].Value.ToString();
                uiflamp5.Text = uiDGView["flamp5", rowindex].Value.ToString();
                uifon.Text = uiDGView["fon", rowindex].Value.ToString();
                uifcloseflag.Text = uiDGView["foff", rowindex].Value.ToString();
                uifairIP.Text = uiDGView["fairIP", rowindex].Value.ToString();
                uifairPORT.Text = uiDGView["fairPORT", rowindex].Value.ToString();
                uifNoOrder.Text = uiDGView["fNoOrder", rowindex].Value.ToString();
                uifYesOrder.Text = uiDGView["fYesOrder", rowindex].Value.ToString();
                uifpoint_size.Text = uiDGView["fpoint_size", rowindex].Value.ToString();
                uifnum.Text = uiDGView["fnum", rowindex].Value.ToString();
                uifweight.Text = uiDGView["fweight", rowindex].Value.ToString();
                uifcloseflag.Text = uiDGView["fcloseflag", rowindex].Value.ToString();

                int i = 0;
                string sstatus = uiDGView["fstate", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstate.Items)
                {
                    string id = dr["fstateno"].ToString();
                    if (id.Equals(sstatus))
                    {
                        uifstate.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfpoint_no = uiDGView["fpoint_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifpoint_no.Items)
                {
                    string id = dr["fpoint_no"].ToString();
                    if (id.Equals(sfpoint_no))
                    {
                        uifpoint_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfiptype = uiDGView["fiptype", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifiptype.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfiptype))
                    {
                        uifiptype.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfstorageflag = uiDGView["fstorageflag", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstorageflag.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfstorageflag))
                    {
                        uifstorageflag.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifequipmentbarcode.ReadOnly = true;

            base.OnSys02();
        }


        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fequipmenttag", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fequipmentbarcode", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'i_ts_warehouse','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();
            
            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifequipmentbarcode, "请输入固定资产码")
                   && CheckEmpty(uifequipmenttag, "请输入设备名称")
                   && CheckEmpty(uifmacno, "请输入设备序列号")
                   && CheckEmpty(uifpoint_no, "请选择存放地点")
                   && CheckEmpty(uifstate, "请选择状态")
                   && CheckEmpty(uifstorageflag, "请选择存储分类")
                   && CheckEmpty(uifrfidid, "请输入RFIDLINE")
                   && CheckEmpty(uifnum, "请输入陈列个数")
                   && CheckEmpty(uifweight, "请输入最大承受重量");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifstate.SelectedItem;
            string sstatus = drv1.Row["fstateno"].ToString();

            DataRowView drv2 = (DataRowView)uifpoint_no.SelectedItem;
            string sfpoint_no = drv2.Row["fpoint_no"].ToString();

            DataRowView drv3 = (DataRowView)uifstorageflag.SelectedItem;
            string sfstorageflag = drv3.Row["fDictNo"].ToString();

            DataRowView drv4 = (DataRowView)uifiptype.SelectedItem;
            string sfiptype= drv4.Row["fDictNo"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        i_ts_warehouse myData = context.i_ts_warehouse.Find(uifequipmentbarcode.Text);
                        if (myData is null)
                        {
                            i_ts_warehouse myData2 = new i_ts_warehouse();
                            myData2.fequipmentbarcode = uifequipmentbarcode.Text;
                            myData2.feuipmentid = 15;
                            myData2.fequipmenttag = uifequipmenttag.Text;
                            myData2.fmacno = uifmacno.Text;
                            myData2.frfidIP = uifrfidIP.Text;
                            myData2.frfidPort = uifrfidPort.Text;
                            myData2.frfidid = int.Parse(uifrfidid.Text);
                            myData2.fip = uifip.Text;
                            myData2.fport = uifport.Text;
                            myData2.flamp1 = uiflamp1.Text;
                            myData2.flamp2 = uiflamp2.Text;
                            myData2.flamp3 = uiflamp3.Text;
                            myData2.flamp4 = uiflamp4.Text;
                            myData2.flamp5 = uiflamp5.Text;
                            myData2.fon = uifon.Text;
                            myData2.foff = uifcloseflag.Text;
                            myData2.fairIP = uifairIP.Text;
                            myData2.fairPORT = uifairPORT.Text;
                            myData2.fYesOrder = uifYesOrder.Text;
                            myData2.fNoOrder = uifNoOrder.Text;
                            myData2.fpoint_size = uifpoint_size.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fsite_no = sSite_No;
                            myData2.fstate = int.Parse(sstatus);
                            myData2.fpoint_no = sfpoint_no;
                            myData2.fstorageflag = sfstorageflag;
                            myData2.fnum = int.Parse(uifnum.Text);
                            myData2.fweight = int.Parse(uifweight.Text);
                            myData2.fcloseflag = uifcloseflag.Text;
                            myData2.fiptype = int.Parse(sfiptype);
                            context.i_ts_warehouse.Add(myData2);
                            context.SaveChanges();
                            doProc();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    i_ts_warehouse myts_warehouse = context.i_ts_warehouse.Find(uifequipmentbarcode.Text);

                    myts_warehouse.fequipmenttag = uifequipmenttag.Text;
                    myts_warehouse.fmacno = uifmacno.Text;
                    myts_warehouse.frfidIP = uifrfidIP.Text;
                    myts_warehouse.frfidPort = uifrfidPort.Text;
                    myts_warehouse.frfidid = int.Parse(uifrfidid.Text);
                    myts_warehouse.fip = uifip.Text;
                    myts_warehouse.fport = uifport.Text;
                    myts_warehouse.flamp1 = uiflamp1.Text;
                    myts_warehouse.flamp2 = uiflamp2.Text;
                    myts_warehouse.flamp3 = uiflamp3.Text;
                    myts_warehouse.flamp4 = uiflamp4.Text;
                    myts_warehouse.flamp5 = uiflamp5.Text;
                    myts_warehouse.fon = uifon.Text;
                    myts_warehouse.foff = uifcloseflag.Text;
                    myts_warehouse.fairIP = uifairIP.Text;
                    myts_warehouse.fairPORT = uifairPORT.Text;
                    myts_warehouse.fYesOrder = uifYesOrder.Text;
                    myts_warehouse.fNoOrder = uifNoOrder.Text;
                    myts_warehouse.fpoint_size = uifpoint_size.Text;
                    myts_warehouse.foperator = sUser;
                    myts_warehouse.fupddate = DateTime.Now;
                    myts_warehouse.fstate = int.Parse(sstatus);
                    myts_warehouse.fpoint_no = sfpoint_no;
                    myts_warehouse.fstorageflag = sfstorageflag;
                    myts_warehouse.fnum = int.Parse(uifnum.Text);
                    myts_warehouse.fweight = int.Parse(uifweight.Text);
                    myts_warehouse.fcloseflag = uifcloseflag.Text;
                    myts_warehouse.fiptype = int.Parse(sfiptype);
                    int result = context.SaveChanges();
                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uiCBfstorageflag_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
